package sc
package test

import scala.io.Source

object test5 {
  def main(args: Array[String]): Unit = {
    // 3、统计每个班级的平均分
    var bs = Source.fromFile("scala/data/score.txt")
    var cs = Source.fromFile("scala/data/students.csv")
    var scores = bs.getLines().toList
    var students = cs.getLines().toList
    val studentMap = students.map(e => {
      val sp = e.split(",")
      (sp(0), sp(4))
    }).groupBy {
      case (_, clazz) => clazz
    }.map {
      case (clazz, list) =>
        (clazz, list.map {
          case (id, _) => id
        })
    }
    val scoreMap = scores.map(e => {
      val sp = e.split(",")
      (sp(0), sp(2))
    }).groupBy {
      case (id, _) => id
    }.map {
      case (id, list) =>
        (id, list.map {
          case (id, score) => score.toDouble
        }.sum)
    }

    val res = studentMap.map {
      case (clazz, ids) =>
        (clazz, ids.map(e => {
          val dou: Double = scoreMap.getOrElse(e, 0)
          dou
        }).sum./(ids.size))
    }

    println(studentMap)
    println(scoreMap)
    println(res)
  }
}
